#include "common.h"
#include "regs.h"
#include "types.h"

/*********************************************************************************************************
  寄存器偏移
*********************************************************************************************************/
#define TIMER_IRQ_EN   0x00
#define TIMER_IRQ_STA  0x04
#define TIMER0_CTRL    0x10
#define TIMER0_INTV    0x14
#define TIMER0_CUR     0x18
#define TIMER1_CTRL    0x20
#define TIMER1_INTV    0x24
#define TIMER1_CUR     0x28

#define TIMER_FREQ     (24 * 1000 * 1000 / 2)

void os_timer_start(unsigned int tick)
{
    u32 count = TIMER_FREQ / tick;

    write32(count, TIMER_BASE + TIMER0_INTV);
    write32((1 << 1) | (1 << 2) | (1 << 4), TIMER_BASE + TIMER0_CTRL);
    while ((read32(TIMER_BASE + TIMER0_CTRL) >> 1) & 0x01);
    write32(read32(TIMER_BASE + TIMER0_CTRL) | (1 << 0), TIMER_BASE + TIMER0_CTRL);
    write32(read32(TIMER_BASE + TIMER_IRQ_EN) | (1 << 0), TIMER_BASE + TIMER_IRQ_EN);
}

void os_timer_int_clear(void)
{
    write32(read32(TIMER_BASE + TIMER_IRQ_STA) | (1 << 0), TIMER_BASE + TIMER_IRQ_STA);
}